﻿using UnityEngine;

public class MyNetWork : MonoBehaviour
{
    /*/// <summary>
    /// 最大连接数
    /// </summary>
    public int connections = 10;
    /// <summary>
    /// 监听端口
    /// </summary>
    public int listenPort = 25002;

    /// <summary>
    /// 服务器初始化完成,或者  客户端连接到了服务器之后 调用
    /// </summary>
    public void Init()
    {
    }
    /// <summary>
    /// 创建服务器
    /// </summary>
    public void CreateServer()
    {
        OnFailedToConnectToMasterServer(Network.InitializeServer(connections, listenPort, !HavePublicAddress()));
    }
    /// <summary>
    /// 连接到服务器
    /// </summary>
    /// <param name="ip"></param>
    /// <param name="port"></param>
    public void Connect(string ip, int port)
    {
        OnFailedToConnectToMasterServer(Network.Connect(ip, port));
    }
    /// <summary>
    /// 服务器初始化后调用
    /// </summary>
    public void OnServerInitialized()
    {
        NGUIDebug.Log("服务器初始化");
        Init();
    }
    /// <summary>
    /// 每当一个新玩家成功连接时，在服务器上调用这个函数。 
    /// </summary>
    /// <param name="player"></param>
    public void OnPlayerConnected(NetworkPlayer player)
    {
        NGUIDebug.Log("新的玩家链接进来了");
    }
    /// <summary>
    /// 每当一个玩家从服务器断开时，在服务器调用这个函数。
    /// </summary>
    /// <param name="player"></param>
    public void OnPlayerDisconnected(NetworkPlayer player)
    {
        NGUIDebug.Log("一个玩家断开了");
    }
    /// <summary>
    /// 当成功连接到服务器时，在客户端调用这个函数。
    /// </summary>
    public void OnConnectedToServer()
    {
        NGUIDebug.Log("玩家链接到服务器了");
        Init();
    }
    /// <summary>
    /// 链接到服务器 回调
    /// </summary>
    /// <param name="error"></param>
    public void OnFailedToConnectToMasterServer(NetworkConnectionError error)
    {
        switch (error)
        {
            case NetworkConnectionError.AlreadyConnectedToAnotherServer:
                break;
            case NetworkConnectionError.AlreadyConnectedToServer:
                break;
            case NetworkConnectionError.ConnectionBanned:
                break;
            case NetworkConnectionError.ConnectionFailed:
                break;
            case NetworkConnectionError.CreateSocketOrThreadFailure:
                break;
            case NetworkConnectionError.EmptyConnectTarget:
                break;
            case NetworkConnectionError.IncorrectParameters:
                break;
            case NetworkConnectionError.InternalDirectConnectFailed:
                break;
            case NetworkConnectionError.InvalidPassword:
                break;
            case NetworkConnectionError.NATPunchthroughFailed:
                break;
            case NetworkConnectionError.NATTargetConnectionLost:
                break;
            case NetworkConnectionError.NATTargetNotConnected:
                break;
            case NetworkConnectionError.NoError://没有错误
                EDebug.Log("创建局域网服务器或者连接 成功没有错误");
                break;
            case NetworkConnectionError.RSAPublicKeyMismatch:
                break;
            case NetworkConnectionError.TooManyConnectedPlayers:
                break;
            default:
                break;
        }
    }
    /// <summary>
    /// 获取外网ip地址
    /// </summary>
    public string GetExternalIP()
    {
        return Network.player.externalIP;
    }
    /// <summary>
    /// 获取本机局域网ip地址
    /// </summary>
    public string GetLocalIP()
    {
        return Network.player.ipAddress;
    }
    /// <summary>
    /// 是否有公网地址
    /// </summary>
    public bool HavePublicAddress()
    {
        return Network.HavePublicAddress();
    }
    /// <summary>
    /// 在局域网中 创建物体 
    /// </summary>
    /// <param name="prefab">物体预设</param>
    /// <param name="posiotn">位置</param>
    /// <param name="rotation">角度</param>
    public GameObject Instantiate(GameObject prefab, Vector3 posiotn, Quaternion rotation)
    {
        prefab.AddMissingComponent<NetworkView>();
        int group = int.Parse(Network.player + "");
        Object go = Network.Instantiate(prefab, posiotn, rotation, group);
        return (GameObject)go;
    }
    /// <summary>
    /// 调用物体 远程方法  
    /// </summary>
    /// <param name="go">该物体</param>
    /// <param name="FunStr">物体上远程方法名字</param>
    /// <param name="mode">默认发送给所有主机上的该物体</param>
    /// <param name="args">参数, 可用的RPC参数为int，float，string，NetworkPlayer，NetworkViewID，Vector3和Quaternion</param>
    public void RCP(GameObject go, string FunStr, RPCMode mode = RPCMode.AllBuffered, params object[] args)
    {
        go.GetComponent<NetworkView>().RPC(FunStr, mode, args);
    }*/
}